import App from './App'

// #ifndef VUE3
import Vue from 'vue'
import './uni.promisify.adaptor'
Vue.config.productionTip = false
App.mpType = 'app'

//uView配置
// main.js
import uView from "uview-ui";
Vue.use(uView);


try {
	function isPromise(obj) {
		return (
			!!obj &&
			(typeof obj === "object" || typeof obj === "function") &&
			typeof obj.then === "function"
		);
	}


	// 统一 vue2 API Promise 化返回格式与 vue3 保持一致
	uni.addInterceptor({
		returnValue(res) {
			if (!isPromise(res)) {
				return res;
			}
			return new Promise((resolve, reject) => {
				res.then((res) => {
					if (res[0]) {
						reject(res[0]);
					} else {
						resolve(res[1]);
					}
				});
			});
		},
	});

	uni.addInterceptor('request', {
		invoke(args) {
			args.header = {
				Accept: "application/json"
			}
			// request 触发前拼接 url 
			args.url = 'https://7529yh2880.zicp.fun' + args.url
			//读取token
			//同步方法失效，使用异步
			// uni.getStorage({
			// 	key: 'token',
			// 	success: function(res) {
			// 		if (res.data) {
			// 			console.log(res.data)
			// 			// args.header = {},
			// 			args.header.token = res.data
			// 		}
			// 	}
			// });
			// 在制作token之前不能打开
			let token = uni.getStorageSync('token');
			if (token) {
				console.log(token);
				args.header.token = token;
			} else {
				uni.redirectTo({
					url: "pages/loginByAccount"
				})
			}
		},
		success(args) {
			// 请求成功后，修改code值为1
			//args.data.code = 1
			if (args.header.code == 405) {
				console.log(args.header.code)
				uni.reLaunch({
					url: "pages/loginByAccount"
				})
			}
		},
		fail(err) {
			// console.log('interceptor-fail',err)
			uni.redirectTo({
				url: "pages/loginByAccount"
			})
		},
		complete(res) {
			// console.log('interceptor-complete',res)
		}
	});
} catch (error) {}



const app = new Vue({
	...App
})
app.$mount()
// #endif


// #ifdef VUE3
import {
	createSSRApp
} from 'vue'
export function createApp() {
	const app = createSSRApp(App)
	return {
		app
	}
}
// #endif